<?php
namespace app\api\controller;

use app\api\model\Evalutedesc;
use app\api\model\Notice;
use think\Cache;
use think\Controller;
use app\api\model\Evalute;
use think\Exception;
use think\Request;

/**
 * Class Evalutes
 * 主评论控制器
 */
class Evalutes extends Controller
{
    /**
     * 获取全部的评论
     */
    public function getEvalutes()
    {
        $re = Evalute::getEvalutes();
        if(!$re){
            AjaxReturn('0','请求内容不存在');
        }else{
            AjaxReturn('1','请求成功',$re);
        }
    }
    //获取单条评论
    public function getEvaluteById()
    {
        $id=$_GET['id'];
        $userid=$_GET['userid'];
        $re = Evalute::getEvaluteById($id);
        if(!$re){
            AjaxReturn('0','请求内容不存在');
        }else{
            if($re->userid!=$userid){
                $re->browsenum = $re->browsenum+1;
                $re->save();
            }
            if(!$re){
                AjaxReturn('0','请求内容不存在');
            }else{
                AjaxReturn('1','请求成功',$re);
            }
        }
    }
    public function test(){
        $res=Evalute::addEvalute(28);
        $belongid=$res->userid;
        echo $belongid;
    }
    //点赞和取消赞
    public function agree()
    {
        $userid=$_GET['userid'];
        $id=$_GET['id'];
        $re=Evalute::getEvaluteById($id);
        $str=$re->agreeusers;
        if(strripos($str,'|'.$userid.'|')===false){
            $str.='|'.$userid.'|';
            try{
                $re->agreeusers=$str;
                $re->save();
                Notice::addevalutenotice($re->id,'赞了你',$userid,$re->userid);
                AjaxReturn('1','点赞成功');
            }catch (Exception $exception){
                AjaxReturn('0','点赞失败');
            }
        }else{
            $str=str_replace('|'.$userid.'|','',$str);
            try{
                $re->agreeusers=$str;
                $re->save();
                Notice::delevalutenotice($re->id,'赞了你',$userid,$re->userid);
                AjaxReturn('1','取消赞成功');
            }catch (Exception $exception){
                AjaxReturn('0','取消赞失败');
            }
        }
    }
    //添加回复
    public function addreply(){
        $request=Request::instance();
        $data=$request->post();
        $re=(new \app\api\validate\Evalute())->goCheck();
        if($re===true){
            $re=Evalutedesc::addreply($data);
            if(!$re){
                AjaxReturn('0','评论失败');
            }else{
                AjaxReturn('1','评论成功');
            }
        }else{
            AjaxReturn('90001',$re);
        }
    }
    /**
     * @desc 发表评论
     */
    public function addevalute()
    {
        $audit=Cache::remember('audit',function(){
            return 0;
        });
        if($audit==0){
            $msg='发表成功,等待审核';
        }else if($audit==1){
            $msg='发表成功';
        }
        $request=Request::instance();
        $data=$request->post();
        $re=(new \app\api\validate\Evalute())->goCheck();
        if($re===true){
            $file = request()->file('file');
            if(!empty($file)){
                if($file->checkSize(10485760)){
                    $desc=$file->getInfo();
                    $video=array('video/mp4');
                    $image=array('image/jpeg','image/png','image/jpg','image/gif','image/swf');
                    if(in_array($desc['type'],$image)){
                        $filetype='image';
                        $filePath= ROOT_PATH . 'public\uploadEvalute\image';
                        if(!file_exists($filePath)){
                            mkdir($filePath);
                        }
                        $oldFileName=$desc['tmp_name'];
                        $Ext=explode('.',$desc['name']);
                        $Ext='.'.end($Ext);
                        $fileName=time().rand(1000,9999).$Ext;
                        $path=$filePath.'\\'.$fileName;
                        if(move_uploaded_file($oldFileName,$path)){
                            $re=Evalute::addEvalute($data,$audit,$filetype,$fileName);
                            if($re){
                                AjaxReturn('1',$msg);
                            }else{
                                AjaxReturn('0','发表失败');
                            }
                        }else{
                            AjaxReturn('90002',$file->getError());
                        }
                    }else if(in_array($desc['type'],$video)){
                        $filetype='video';
                        $filePath= ROOT_PATH . 'public\uploadEvalute\video';
                        if(!file_exists($filePath)){
                            mkdir($filePath);
                        }
                        $oldFileName=$desc['tmp_name'];
                        $Ext=explode('.',$desc['name']);
                        $Ext='.'.end($Ext);
                        $fileName=time().rand(1000,9999).$Ext;
                        $path=$filePath.'\\'.$fileName;
                        if(move_uploaded_file($oldFileName,$path)){
                            $re=Evalute::addEvalute($data,$audit,$filetype,$fileName);
                            if($re){
                                AjaxReturn('1',$msg);
                            }else{
                                AjaxReturn('0','发表失败');
                            }
                        }else{
                            AjaxReturn('90002',$file->getError());
                        }
                    }else{
                        AjaxReturn('90002','文件格式不合法');
                    }
                }else{
                    AjaxReturn('90002','文件大小超过10M');
                }
            }else{
                AjaxReturn('90002','缺少文件');
            }
        }else{
            AjaxReturn('90001',$re);
        }
    }
}
?>